/* 
 * File:   main.cpp
 * Author: yanex
 */

#include <iostream>
#include <cstdio>
#include "TitleList.h"
#define VERSION "0.2"
#define TITLE "anic " << VERSION << "\n"

using namespace std;

// String comparator class
// operator[] returns 0 if strings aren't the same.
class Tester {
    public:
        Tester(string init) : arg(init) {}
        int operator[](char *comp) {
            return (!arg.compare(comp));
        }
    private:
        string arg;
};

//About info output
void habout(std::ostream& stream) {
    stream << TITLE;
    stream << "by yanex, http://yanex.org/\n";
}

//Version info output
void hversion(std::ostream& stream) {
    stream << VERSION << '\n';
}

//Help info output
void hhelp(std::ostream& stream) {
    habout(stream);
    stream << "\nusing:\n" <<
              "-n%l       count of titles (valid: a/w/d)\n" <<
              "-t%l       total watched time (valid: m/h/d)\n" <<
              "-f %s     check title in the list\n" <<
              "-a %p %s  add title to the list\n" <<
              "-s %p %s  set new title options\n" <<
              "-pa       print all titles\n" <<
              "-pw       print watched titles\n" <<
              "-pt       print todo\n" <<
              "-h        print help\n" <<
              "-v        print version\n";

}

int main(int argc, char** argv) {
    if (argc<2) {
        hhelp(cout);
        return 0;
    }
    
    //use "list.txt" if environment var is not set
    string path = getenv("ANIC");
    if (path.length()<2)
        path="list.txt";
    
    TitleList *list = new TitleList(path);
    Tester test(argv[1]);
    
    if (test["-v"] || test["--version"])
        hversion(cout);
    else if (test["-nw"])
        cout << "watched: " << list->count(ANIME_WATCHED) << '\n';
    else if (test["-na"])
        cout << "all: " << list->count(ANIME_ALL) << '\n';
    else if (test["-nd"])
        cout << "todo: " << list->count(ANIME_TODO) << '\n';
    else if (test["-tm"])
        cout << "total: " << list->time(TIME_MINUTE) << "m\n";
    else if (test["-th"])
        cout << "total: " << list->time(TIME_HOUR) << "h\n";
    else if (test["-td"])
        cout << "total: " << list->time(TIME_DAY) << "d\n";
    else if (test["-tM"])
        cout << "total: " << list->time(TIME_MONTH) << "d\n";
    else if (test["-ty"])
        cout << "total: " << list->time(TIME_YEAR) << "d\n";
    else if (test["-pa"])
        list->print(ANIME_ALL, cout);
    else if (test["-pw"])
        list->print(ANIME_WATCHED, cout);
    else if (test["-pt"])
        list->print(ANIME_TODO, cout);
    else if (test["-f"]) {
        if (argc<3)
            hhelp(cout);
        else
            list->check(argv[2], cout);
    } else if (test["-a"]) {
        if (argc<4)
            hhelp(cout);
        else
            list->add(argv[2], argv[3]);
    } else if (test["-s"]) {
        if (argc<4)
            hhelp(cout);
        else
            list->set(argv[2], argv[3]);
    } else
        hhelp(cout);
    
    return 0;
}
